package com.lwz.laboratory.controller;



import com.lwz.laboratory.entity.Department;
import com.lwz.laboratory.entity.Technician;
import com.lwz.laboratory.entity.User;
import com.lwz.laboratory.service.DepartmentService;
import com.lwz.laboratory.service.TechnicianService;
import com.lwz.laboratory.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@Controller
public class TechnicianController {
    @Autowired
    TechnicianService technicianService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    UserService userService;

    @GetMapping("/technicianlist")
    public String technicianlist(Model model, HttpServletRequest request){
        String name = request.getParameter("context");
        List<Technician> list= technicianService.getAlltec(name);
        model.addAttribute("technicianlist",list);
        return "/technician/technician_list";
    }

    @RequestMapping("/technicianlist/{id}")
    public String toupdatetechnician(String departmentname,@PathVariable("id")Integer id, Model model, HttpServletRequest request){
        Technician technician=  technicianService.getonetec(id);
        model.addAttribute("technician",technician);
        Collection<Department> department=departmentService.getAlldep(departmentname);
        model.addAttribute("departments",department);
        return "/technician/technician_edit";
    }

    @RequestMapping("/technicianedit")
    public String edittechnician(Technician technician, Model model, HttpServletRequest request){
        //修改技术信息将user表也修改
        User user= userService.findoneUser(technician.getWorkid());
        user.setName(technician.getName());
        user.setPassword(technician.getPassword());
        userService.edituser(user);
        technicianService.Updatetec(technician);
        return "redirect:/technicianlist";
    }

    @RequestMapping("/technicianadd")
    public String toAddtechnician(String departmentname, HttpServletRequest request,Model model){
        List<Department> departments =  departmentService.getAlldep(departmentname);
        model.addAttribute("departments",departments);
        return "/technician/technician_add";
    }

    @RequestMapping("/tecsave")
    public String Addtechnician(Technician technician, HttpServletRequest request){
        //添加技术信息将user表也添加
        User user =new User();
        user.setUserid(technician.getWorkid());
        user.setName(technician.getName());
        user.setPassword(technician.getPassword());
        user.setRolename("技术员");
        userService.adduser(user);
        technicianService.Adddep(technician);
        return "redirect:/technicianlist";
    }


    @RequestMapping("/techniciandel/{id}")
    public String deltechnician(@PathVariable("id")Integer id, Model model, HttpServletRequest request){
        //删除技术信息将user表也删除
        Technician technician =technicianService.getonetec(id);
        userService.deluser(technician.getWorkid());
        technicianService.Deldep(id);
        return "redirect:/technicianlist";
    }

    @RequestMapping("/toadminindex")
    public String toadminindex(){
        return "admin";
    }




}
